<?php

namespace App\Models;

use Dcat\Admin\Traits\HasDateTimeFormatter;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
use Overtrue\Pinyin\Pinyin;

/**
 * App\Models\CarBrand
 *
 * @property int $id
 * @property string $name 品牌名称
 * @property string $prefix 品牌名称
 * @property string $logo 品牌logo
 * @property int $sort 排序
 * @property int $status 状态:0=正常,1=隐藏
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @method static \Illuminate\Database\Eloquent\Builder|CarBrand newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|CarBrand newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|CarBrand query()
 * @method static \Illuminate\Database\Eloquent\Builder|CarBrand whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|CarBrand whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|CarBrand whereName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|CarBrand whereSort($value)
 * @method static \Illuminate\Database\Eloquent\Builder|CarBrand whereStatus($value)
 * @method static \Illuminate\Database\Eloquent\Builder|CarBrand whereUpdatedAt($value)
 * @mixin \Eloquent
 * @method static \Illuminate\Database\Eloquent\Builder|CarBrand whereLogo($value)
 * @method static \Illuminate\Database\Eloquent\Builder|CarBrand wherePrefix($value)
 */
class CarBrand extends Model
{
	use HasDateTimeFormatter;
    protected $table = 'car_brand';

    protected $fillable = ['name','logo'];

    public static function getList(): array
    {
        $list = self::query()->where("status",0)->orderByDesc("sort")->orderByDesc("id")->get(['id','name','prefix']);
//        $pinyin = new Pinyin();
//        foreach ($list as $item){
//            $name = substr($pinyin->permalink($item->name,''),0,1);
//            $item->prefix = ucfirst($name);
//        }
        $newList = [];
        $data = collect($list)->groupBy("prefix")->toArray();
        for ($num = 65;$num<=90;$num++){
            $zm = chr($num);
            if (isset($data[$zm])){
                $newList[] = ['name'=>$zm,'data'=>$data[$zm]];
            }else{
                $newList[] = ['name'=>$zm,'data'=>[]];
            }
        }
        return $newList;
    }

    public static function getOptions(): Collection
    {
        return self::query()->orderByDesc("id")->pluck("name","id");
    }

    public static function getTypesList(): array
    {
        return ['轿车','SUV','MPV'];
    }

    public static function getTypes(): array
    {
        return collect(self::getTypesList())->map(function ($item){
            return ['name'=>$item];
        })->toArray();
    }


    public static function getColors(): array
    {
        return [
            ['name'=> '银灰色'],
            ['name'=> '银色'],
            ['name'=> '灰色'],
            ['name'=> '黑色'],
            ['name'=> '白色'],
            ['name'=> '红色'],
            ['name'=> '蓝色'],
            ['name'=> '黄色'],
            ['name'=> '绿色'],
            ['name'=> '棕色'],
            ['name'=> '紫色'],
            ['name'=> '粉色'],
        ];
    }
}
